package com.atguigu.srb.core.controller.api;


import com.alibaba.fastjson.JSON;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.service.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-06-30
 */
@RestController
@RequestMapping("/api/core/userAccount")
@Api(tags = "hfb充值模块")
public class ApiUserAccountController {
    @Resource
    UserAccountService userAccountService;
    //提现的同步接口
    @ApiOperation("用户提现")
    @PostMapping ("/withdraw/{amount}")
    public R withDraw(@RequestHeader(value = "token",required = false)String token,
                      @PathVariable("amount")BigDecimal amount){
        String form = userAccountService.withDraw(token,amount);
        return R.ok().data("form",form);
    }
    //提现的异步接口 "http://192.168.216.100/api/core/userAccount/notifyWithdraw"
    @ApiOperation("hfb提现后的异步通知接口")
    @PostMapping("/notifyWithdraw")
    public String callbackNotify(@RequestParam Map<String,Object> map){
        // System.out.println(JSON.toJSONString(map));
        //接收hfb传过来的请求参数,使用map进行接收
        return userAccountService.withDrawCallback(map);
    }
    //计算预计的收益
    @ApiOperation("计算投资人预计的收益")
    @GetMapping("/{period}/{yearRate}/{returnMethod}/{amount}")
    public R expectAmount (@PathVariable("period")Integer period,
                           @PathVariable("yearRate")BigDecimal yearRate,
                           @PathVariable("returnMethod")Integer returnMethod,
                           @PathVariable("amount")BigDecimal amount){
        BigDecimal expectAmount = userAccountService.expectAmount(period,yearRate,returnMethod,amount);
        return R.ok().data("expectAmount",expectAmount);
    }
    //查询账户的余额
    @ApiOperation("根据用户的token查询账户余额")
    @GetMapping("/getAmount")
    public R getAmount(@RequestHeader("token")String token){
        //获取userId
        Long userId = JwtUtils.getUserId(token);
        //根据userId查询用户的amount
        BigDecimal amount = userAccountService.getAmount(userId);
        return R.ok().data("amount",amount);
    }
    //充值的接口 TODO 这个地方增加auth,防止用户没有登录直接来充值
    @PostMapping("/recharge/{amount}")
    @ApiOperation("处理用户充值请求")
    public R handleRecharge(@PathVariable("amount")BigDecimal amount,
                      @RequestHeader(value = "token",required = false)String token){
        String form = userAccountService.handleRecharge(amount,token);
        return R.ok().data("form",form);
    }
    //hfb回调的接口
    @PostMapping("/notify")
    @ApiOperation("充值完成后的回调的接口")
    public String rechargeCallback(HttpServletRequest request){
        //下面是获取hfb返回来的参数的方法1
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> params = RequestHelper.switchMap(parameterMap);
        //下面是获取hfb返回回来的参数的方法2
            /* @PostMapping("/notify")
        @ApiOperation("充值完成后的回调的接口")
        public String rechargeCallback(@RequestParam Map<String,Object> params){
            return userAccountService.callback(params);
        } */
        //两种获取参数的方法都可以,我偏向第二种,比较的方便
        return userAccountService.callback(params);
    }



}

